package com.shengdou.as.slopsapptest.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;

import com.shengdou.as.slopsapptest.R;
import com.shengdou.as.slopsapptest.base.BaseActivity;
import com.shengdou.as.slopsapptest.utils.Constants;
import com.shengdou.as.slopsapptest.utils.HttpUtils;
import com.shengdou.as.slopsapptest.utils.LogUtil;
import com.shengdou.as.slopsapptest.utils.NetConstant;
import com.shengdou.as.slopsapptest.utils.SharedPreferenceUtil;
import com.shengdou.as.slopsapptest.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by AS on 2016/11/30.
 * 故障申报
 */

public class FailureQuestionActivity extends BaseActivity {
    private EditText mUserInfo;
    private String factoryName, phone;
    private String info;
    private String pool_name;
    private String real_name;

    private Map<String, String> params = new HashMap<>();

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constants.STOPGUARANTEE:
                    String results = msg.obj.toString();
                    LogUtil.e("result", results);
                    params.clear();
                    try {
                        JSONObject jsonObject = new JSONObject(results);
                        ToastUtils.showToast(jsonObject.getString("msg"));
                        finish();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.failure_question);
        init();
    }

    private void init() {
        phone = SharedPreferenceUtil.getPreferenceString(FailureQuestionActivity.this, "mobile");
        factoryName = SharedPreferenceUtil.getPreferenceString(FailureQuestionActivity.this, "FactoryName");
        real_name = SharedPreferenceUtil.getPreferenceString(FailureQuestionActivity.this, "real_name");
        pool_name = getIntent().getStringExtra("pool_id");
        mUserInfo = (EditText) findViewById(R.id.ET_DEIT);
    }

    public void orderClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.txt_submit:
                info = mUserInfo.getText().toString().trim();
                if (info == null || info.equals("")) {
                    ToastUtils.showToast("请填写申报信息");
                    return;
                }
                params.put("mobile", phone);
                params.put("factory_name", factoryName);
                params.put("pool_name", pool_name);
                params.put("user_name", real_name);
                params.put("info", info);
                LogUtil.e(factoryName + pool_name + real_name + info);
                HttpUtils.postFactory(mHandler, NetConstant.STOPGUARANTEE, params, Constants.STOPGUARANTEE, FailureQuestionActivity.this);
                break;
        }
    }

}
